package com.mojo.collection.map;

import java.util.Collection;
import java.util.Objects;
import java.util.TreeMap;

/**
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/1/26 11:22
 */
public class TreeMapDemo {
    public static void main(String[] args) {
        TreeMap<Person, Integer> map = new TreeMap<>((o1, o2) -> o2.name.compareTo(o1.name));
        map.put(new Person("aaa"), 11);
        map.put(new Person("cc"), 22);
        map.put(new Person("dd"), 33);
        map.put(new Person("ccc"), 44);
        Collection<Integer> values = map.values();
        for (Integer value : values) {
            //根据key排序
            System.out.println(value);
        }

    }

    private static class Person {
        private String name;

        public Person(String name) {
            this.name = name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {

                return true;
            }
            if (o == null || getClass() != o.getClass()) {

                return false;
            }
            Person person = (Person) o;
            return Objects.equals(name, person.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name);
        }
    }
}
